﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Basics;
using YiSha.Entity;
using YiSha.Enums;
using YiSha.Service;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Admin.Web.Controllers
{
    [AllowAnonymous]
    public class FileController : BaseController
    {
        public FileService FileService { get; set; }
        #region 上传单个文件
        [HttpPost]
        public async Task<TData<SysImageInfo>> UploadFile(int fileModule, [FromForm] IFormCollection fileList)
        {
            TData<SysImageInfo> obj = await FileService.UploadFile(fileModule, fileList.Files);
            return obj;
        }
        #endregion

        #region 删除文件
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public TData DeleteFile(string ids)
        {
            var obj = FileService.DeleteFile(ids);
            return obj;
        }
        /// <summary>
        /// 删除多个文件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public TData DeleteFileMultiple(List<string> ids)
        {
           var obj = FileService.DeleteFile(ids);
            return obj;
        }
        #endregion

        #region 下载文件
        [HttpGet]
        public FileContentResult DownloadFile(string filePath, int delete = 1)
        {
            TData<FileContentResult> obj = FileService.DownloadFile(filePath, delete);
            if (obj.MsgCode == MsgCode.Success)
            {
                return obj.Data;
            }
            else
            {
                throw new Exception("下载失败：" + obj.Msg);
            }
        }
        #endregion
    }
}